<?php

namespace App\Admin\Controllers;

use App\Models\Order;

use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ShareController extends Controller
{
    use ModelForm;
    protected $hader = '分享管理';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->hader);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->hader);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->hader);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->model()->where('parent_user_id','>',0)
                ->whereRaw('`pay_time` IS NOT NULL')
                ->orderBy('id','desc');
            $grid->id('ID')->sortable();
            $grid->sn('订单号');
            $grid->username('姓名');
            $grid->mobile('电话');
            $grid->column("","合同倒计时")->display(function(){
                return '';
            });
            $grid->column("","分享人")->display(function(){
                return User::find($this->parent_user_id)->mobile;
            });
            $grid->column("apply_status","状态")->display(function(){
                switch ($this->apply_status) {
                    case Order::APPLY_STATUS_PASS:
                        return '已提现';
                    case Order::APPLY_STATUS_WAIT:
                        return '未提现';
                    case Order::APPLY_STATUS_WAITING:
                        return '已提现';
                }
            });
            $grid->filter(function(Grid\Filter $filter){
                // 禁用id查询框
                $filter->disableIdFilter();
                $filter->where(function ($query) {
                    $user = User::where('mobile',$this->input)->first();
                    $query->where('parent_user_id',$user->id);

                }, '分享人手机号');
            });
            $grid->actions(function(Grid\Displayers\Actions $actions){
                $actions->disableDelete();
                $actions->disableEdit();
            });
            $grid->pay_time('购买时间');
            $grid->column("full_address","地址")->display(function(){
                return $this->address . ' '.$this->street;
            });
            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
